package com.rustic.server.service.impl;

import cn.xy.commons.enums.DisableStatus;
import cn.xy.commons.exception.JzRuntimeException;
import com.rustic.server.enums.LinkType;
import com.rustic.server.po.Attractions;
import com.rustic.server.po.EventActivity;
import com.rustic.server.po.Food;
import com.rustic.server.po.Rustic;
import com.rustic.server.po.TravelPlan;
import com.rustic.server.po.UserCollectionLink;
import com.rustic.server.po.UserLikeLink;
import com.rustic.server.service.AttractionsService;
import com.rustic.server.service.CommentService;
import com.rustic.server.service.EventActivityService;
import com.rustic.server.service.FoodService;
import com.rustic.server.service.RusticService;
import com.rustic.server.service.TravelPlanService;
import com.rustic.server.service.UserBrowseLinkService;
import com.rustic.server.service.UserCollectionLinkService;
import com.rustic.server.service.UserLikeLinkService;
import com.rustic.server.vo.UserInteractionStatusVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private RusticService rusticService;

    @Autowired
    private AttractionsService attractionsService;

    @Autowired
    private FoodService foodService;

    @Autowired
    private TravelPlanService travelPlanService;

    @Autowired
    private EventActivityService eventActivityService;

    @Autowired
    private UserCollectionLinkService userCollectionLinkService;

    @Autowired
    private UserLikeLinkService userLikeLinkService;

    @Autowired
    private UserBrowseLinkService userBrowseLinkService;

    @Override
    public int addInteractionDataCheck(int targetId, int type) {
        int rusticId = 0;
        if (type == LinkType.RUSTIC.getValue()) {
            // 乡村
            Rustic rustic = rusticService.getById(targetId);
            if (rustic == null) {
                throw new JzRuntimeException(RusticService.RUSTIC_NOT_EXIST);
            }
            if (rustic.getStatus() == DisableStatus.DISABLE.getValue()) {
                throw new JzRuntimeException("乡村已经被禁用");
            }
            rusticId = rustic.getId();
        }
        if (type == LinkType.ACTIVITY.getValue()) {
            // 活动事件
            EventActivity eventActivity = eventActivityService.getById(targetId);
            if (eventActivity == null) {
                throw new JzRuntimeException(EventActivityService.EVENT_ACTIVITY_NOT_EXIST);
            }
            if (eventActivity.getStatus() == DisableStatus.DISABLE.getValue()) {
                throw new JzRuntimeException("活动事件已被下架");
            }
            rusticId = eventActivity.getRusticId();
        }
        if (type == LinkType.ATTRACTIONS.getValue()) {
            // 景点
            Attractions attractions = attractionsService.getById(targetId);
            if (attractions == null) {
                throw new JzRuntimeException(AttractionsService.ATTRACTIONS_NOT_EXIST);
            }
            if (attractions.getStatus() == DisableStatus.DISABLE.getValue()) {
                throw new JzRuntimeException("景点已被下架");
            }
            rusticId = attractions.getRusticId();
        }
        if (type == LinkType.FOOD.getValue()) {
            // 美食
            Food food = foodService.getById(targetId);
            if (food == null) {
                throw new JzRuntimeException(FoodService.FOOD_NOT_EXIST);
            }
            if (food.getStatus() == DisableStatus.DISABLE.getValue()) {
                throw new JzRuntimeException("美食推荐已被下架");
            }
            rusticId = food.getRusticId();
        }
        if (type == LinkType.TRAVEL_PLAN.getValue()) {
            // 攻略
            TravelPlan travelPlan = travelPlanService.getById(targetId);
            if (travelPlan == null) {
                throw new JzRuntimeException(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
            }
            if (travelPlan.getStatus() == DisableStatus.DISABLE.getValue()) {
                throw new JzRuntimeException("攻略已被下架");
            }
            rusticId = travelPlan.getRusticId();
        }
        return rusticId;
    }

    @Override
    public <T extends UserInteractionStatusVO> void setInteractionStatus(T interactionStatus, int userId, int targetId, LinkType type) {
        // 点赞数量
        Long likeCount = userLikeLinkService.getTargetAndType(targetId, type);
        interactionStatus.setLikeCount(likeCount);
        // 收藏数量
        Long collectionCount = userCollectionLinkService.getByTargetAndType(targetId, type);
        interactionStatus.setCollectionCount(collectionCount);
        // 浏览数据
        Long browseCount = userBrowseLinkService.getByTargetAndType(targetId, type);
        interactionStatus.setBrowseCount(browseCount);
        if (userId > 0) {
            UserCollectionLink userCollectionLink = userCollectionLinkService.getByUserTargetAndType(userId, targetId, type.getValue());
            UserLikeLink userLikeLink = userLikeLinkService.getByUserTargetAndType(userId, targetId, type.getValue());
            if (userCollectionLink != null) {
                interactionStatus.setCollection(true);
            }
            if (userLikeLink != null) {
                interactionStatus.setLike(true);
            }
        }
    }

}
